<?php
class Admin_Model_Crafts extends Zend_Db_Table_Abstract
{
	protected $_name = 'crafts';
	
	public function getCrafts() {
        $select = $this->_db->select()
                        ->from($this->_name)
                        ->order('description ASC')
                        ->order('date_created ASC');
                        
        $results = $this->getAdapter()->fetchAll($select);
        return $results;
	}
	
	public function findCrafts($description) {
        $select = $this->_db->select()
                        ->from($this->_name)
                        ->order('description ASC')
                        ->order('date_created ASC');
                              
		if($description) {
			$select->where('lower(description) LIKE ?', '%'.$description.'%');
		}

        $results = $this->getAdapter()->fetchAll($select); 
        
        return $results;
	}

	public function getCraftById($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		
		if (!$row) {
			return false;
		} else {
			return $row->toArray();
		}
	}
	
	public function getCraftByDescription($description)
	{
		$row = $this->fetchRow("description = '".$description."' ");
		
		if (!$row) {
			return false;
		} else {
			return $row->toArray();
		}
	}

	public function addCraft($description,$date_created)
	{
		$data = array(
			'description'	=> $description,
			'date_created' 	=> $date_created,
		);
		$this->insert($data);
	}

	public function updateCraft($id,$description)
	{
		$data = array(
			'description'		=> $description
		);
		$this->update($data, 'id = '. $id);
	}

	public function deleteCraft($id)
	{
		$this->delete('id =' . (int)$id);
	}

	public function isUniqueCraftDescription($description)
	{
		$select = $this->select();
		$select->from($this->_name, 'COUNT(*) AS num')
		->where('description = ?', $description);

		return ($this->fetchRow($select)->num == 0) ? true : false;
	}

}